< Back
# Generate Controller and model for SpringBoot https://github.com/OpenAPITools/openapi-generator maven example https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md ### Dependencies needed ```xml <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <dependency> <groupId>org.openapitools</groupId> <artifactId>jackson-databind-nullable</artifactId> <version>0.2.2</version> </dependency> <dependency> <groupId>io.swagger.core.v3</groupId> <artifactId>swagger-annotations</artifactId> <version>2.1.12</version> </dependency> ``` ### plugin configuration in Pom's project > build > plugins add the configuration like so. ```xml <plugin> <groupId>org.openapitools</groupId> <artifactId>openapi-generator-maven-plugin</artifactId> <version>5.4.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> <configuration> <inputSpec>${project.basedir}/api.yml</inputSpec> <generatorName>spring</generatorName> <output>${project.build.directory}/generated-sources</output> <apiPackage>${project.groupId}.${project.artifactId}.api</apiPackage> <modelPackage>${project.groupId}.${project.artifactId}.dto</modelPackage> <skipOperationExample>true</skipOperationExample> <generateSupportingFiles>false</generateSupportingFiles> <configOptions> <additionalModelTypeAnnotations> @com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL) </additionalModelTypeAnnotations> <sourceFolder>src/gen/java/main</sourceFolder> <interfaceOnly>true</interfaceOnly> <useTags>true</useTags> <skipDefaultInterface>true</skipDefaultInterface> </configOptions> </configuration> </execution> </executions> </plugin> ``` ## Common Problem The api you have is plain and simple. Say we want to add header in parameter and injected bean like `@RegisteredOAuth2AuthorizedClient("keycloak") OAuth2AuthorizedClient authorizedClient` The default generator doesnt' have this much granular setting. So basically we want to transfrom this ```java @RequestMapping( method = RequestMethod.GET, value = "/products", produces = { "application/json" } ) ResponseEntity<Products> getProducts(); ``` into this ```java @RequestMapping( method = RequestMethod.GET, value = "/products", produces = { "application/json" } ) ResponseEntity<Products> getProducts( @RequestHeader HttpHeaders headers, OAuth2AuthorizedClient authorizedClient ); ``` ## Solution One possible solution is to customize template. The above use `openapi-generator-maven-plugin` version `5.4.0` so we need to firstly copy the original template called `api.mustache` from the openapi-generator repo. Which is https://github.com/OpenAPITools/openapi-generator/blob/v5.4.0/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache > Make sure if you are using the identical repo's tag and the dependency version 1. copy the original template above into `resources/openapi/templates/api.mustache` 2. add the following snippet under `<generateSupportingFiles>false</generateSupportingFiles>` and just above `<configOptions>` of the dependency's configuration. ```xml <templateDirectory> src/main/resources/openapi/templates </templateDirectory> ``` 3. In `api.mustache` add import ```java import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; import org.springframework.http.HttpHeaders; ``` 4. Find the line below (it follow `_{{/delegate-method}}{{operationId}}(`) ```java {{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, ``` and replace with ```java @RequestHeader HttpHeaders headers, OAuth2AuthorizedClient authorizedClient {{#allParams}},{{/allParams}}{{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, ``` 5. Regenerate the code i.e. `clean compile -f pom.xml` *That is it. We are all set!* ---